package projectnfc.receiver.hro;

import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;

public class NFCPostToServer extends AsyncTask<String, Void, Boolean>{
	private String serverip;
	private URI serveruri;
	
	public NFCPostToServer(String _serverip){
		serverip = _serverip;
	}
	
	@Override
	protected Boolean doInBackground(String... params) {
		try {
			serveruri = new URI("http://"+serverip+"/incheck/inchecken/"+params[0]+"/"+params[1]);
			DefaultHttpClient httpClient = new DefaultHttpClient();

			HttpPost httpPost = new HttpPost(serveruri);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
}


/*
 * When creating a new instance of this class you must pass in the server ip in the constructor.
 * So the server ip isn't hardcoded. Allows for easier testing in the android app. (EditText field where you can enter server ip)
 * 
 * Android requires this persmission to acces the network:
 * <uses-permission android:name="android.permission.INTERNET" />
 * 
 * If you want to send information to the server do it like this:
 * NFCPostToServerObject.execute(String... params);
 * As parameter supply first the student or teacher name / number and second the room code e.g:
 * NFCPostToServerObject.execute("123456789", "WH.003.021");
 * The method execute comes from the class AsyncTask. Android 3.0 and up requires that network activity
 * goes on a seperate thread than the main thread or you will get an exception.
 * 
 * It returns true if it had no exception. False if it had thrown a exception.
 * Like an unreachable destination.
 * You can get that boolean by using this method: NFCPostToServerObject.get()
 * This method can throw exceptions. So these must be caught.
 *
 * A full example is below:
 
  String studentOrTeacherCode = 1234567890
  String room = WH3.021

		NFCPostToServer server = new NFCPostToServer("192.168.2.8");
			server.execute(studentOrTeacherCode, room);
			try {
				if(server.get())
				{
					tvAdd.setText("All done and succesfull");
				}else
				{
					tvAdd.setText("Exception was thrown. Something went wrong.");
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
			
 */	



/*
 * Old method is below. Which does not run on a seperate thread.
	private void sendCheckInDataToServer(String studentOrTeacher, String room){

		try {

			serveruri = new URI("http://"+serverip+"/NFCCheckIn/NFCCheckIn.svc/sendCheckDataToServer?studentOrTeacher="+studentOrTeacher+"&room="+room);

			DefaultHttpClient httpClient = new DefaultHttpClient();

			HttpPost httpGet = new HttpPost(serveruri);
			HttpResponse httpResponse = httpClient.execute(httpGet);

			 Code below is only used to convert the incoming stream to a string.
			 HttpEntity httpEntity = httpResponse.getEntity();
			 InputStream stream = httpEntity.getContent();
			
			 String result = convertStreamToString(stream);
		 	 result is the reformatted message that the server send back.

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
*/

/*
 * This method is used to convert the incoming stream to a string. Because we only send data it is not necessary.
 * Perhaps for future updates i will keep it here.
private String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
 */
